package com.xxgc.vfc.generator;


import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.sql.Types;
import java.util.Collections;

/**
 * @Author: SJY
 * @Date :2023/9/14 - 09 - 14 - 20:30
 * 代码生成器
 */
public class MyBatisGenerator {
    //生成相关 这三个是不同服务需要修改的
    private static String tableName = "vfc_user_and_roles";
    private static String modelPackageUrl = ".auth";
    private static String model = "volunteer-filling-auth";
    //provider地址 内容提供者地址
    //private static String providerUrl = "volunteer-filling-modules\\volunteer-filling-modules-provider";
    private static String providerUrl = "";

    //表前缀
    private static String tablePrefix = "vfc_";
    //数据相关
    private static String url = "jdbc:mysql://localhost:3306/vfc?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false";
    private static String username = "root";
    private static String password = "123456";
    //项目相关
    private static String author = "风清扬";
    private static String packageUrl = "com.xxgc.vfc";
    //服务名
    private static String modelName = null;

    public static void main(String[] args) {
        packageUrl = packageUrl + modelPackageUrl;
        //获取当前项目路径作为生成路径
        String systemUrlJava = System.getProperty("user.dir")+"\\"+providerUrl+"\\"+model+"\\src\\main\\java\\";
        String systemUrlMapper = System.getProperty("user.dir")+"\\"+providerUrl+"\\"+model+"\\src\\main\\resources\\mapper\\";


        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author(author) // 设置作者
                            .enableSwagger() // 开启 swagger 模式 OpenAPI2
                            //.enableSpringdoc()
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(systemUrlJava); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent(packageUrl) // 设置父包名
                            .moduleName(modelName) // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, systemUrlMapper)); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tableName) // 设置需要生成的表名
                            .addTablePrefix(tablePrefix) // 设置过滤表前缀
                            .entityBuilder()
                            .enableFileOverride();//覆盖Entity

                    builder.entityBuilder()
                            .logicDeleteColumnName("flag")//逻辑删除字段名
                            .addTableFills(//给create_time和update_time生成的时候加注解
                                    new Column("create_time", FieldFill.INSERT),
                                    new Column("update_time", FieldFill.INSERT_UPDATE)
                            )
                            .idType(IdType.ASSIGN_ID)//雪花id
                            .enableLombok();//开启lombok
                })
                .templateConfig(builder -> {//模板配置
                    builder.disable(TemplateType.CONTROLLER);//不生成controller
                    builder.disable(TemplateType.SERVICE);//不生成service
                    builder.disable(TemplateType.SERVICE_IMPL);//不生成service的实现
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
